var parcelorderCtrl  = new XFApp.client();
$.views.converters({
	subString : function(val){
		return parcelorderCtrl.subString(val,7);
	},

    formatDate : function(val){
    	var date = new Date(val);
    	return date.format("yyyy-MM-dd hh:mm:ss");
    }
	
});
XFApp.client.prototype.parcelorder = {
	urlItems : {
		queryLstURL : basePath+"/order/queryLst",
		updateQltURL : basePath+"/order/updateQty",
		delOrOpenURL : basePath+"/order/delOrOpen",
		submitURL : basePath+"/order/submit",
		queryPageURL : basePath+"/order/querySKU",
		updateSKUByIdURL:basePath+"/order/updateSKUById",
		queryGoodsURL:basePath+"/order/queryGoods"
	},	
	
	//清空
	cleanAll : function(idx){
		$("#sku"+idx).val("");			
		$("#itemname"+idx).text("");
	},
	
	queryLst : function(id,type){		
		_this = this;	
		params = {
				awbNo : $("#awbNo").val()
		} 		
		$.ajax({
			url :_this.urlItems.queryLstURL,
			data : params,
			dataType : "json",
			type : 'post',
			success : function(data) {				
				if(data.result.code==10000){
					
					var dataResult = data.result.result ;
					for(key in dataResult){
						if(key == "curtype"){
							dataResult[key] = fCurrencysUtil(data,dataResult[key]);	
						}
					}
					
					var dataList = data.result.result.parcelOrderDetailVoLst;				
					
					$("#head_grid").html($("#dataTmp2").render(dataResult));
	        		$("#page_grid").html($("#dataTmp").render(dataList));
	        		$("#hideDiv").attr({"style":"display:show;"});
	        		
	        		//已生在订单信息不能操作
	                if(!!(data.result.result.transNo)){
	               	 //禁用所有操作功能
	               	 disable();
	                }
	        		
	        		
	        	}else{
	        		$.alert({
        	  	        title: '温馨提示',
        	  	        content: data.result.msg,
        	  	        confirmButtonClass: 'btn-info',
        	  	        confirmButton: '确定',
        	  	        columnClass: 'col-md-4 col-md-offset-4'
        	  	    });
	        	}
			}
		});
	},	
	
	event : function(){
		_this = this;	
		$("#po_search").on("click",function(){
			_this.queryLst();
		});
		
		$("#xClose").on("click",function(){
			$("#choose_sku_modal").hide();
		});
		$("#btnClose").on("click",function(){
			$("#choose_sku_modal").hide();
		});
		
		$("#sku_search").on("click",function(){
			selSkuPage();
		});
		
		$("#sku_reset").on("click",function(){
			$("#itemno").val("");
			$("#itemname").val("");
			$("#customername").val("");			
		});	
		
	},
};

function fCurrencysUtil(data,value){
	if(!value){
		return "";
	}
	var curList = data.currencys.result ;
	for(index in curList){
		var currency = curList[index];
		if(currency.dictcode == value){
			return currency.dictname;
		}
	}
	return  "未知类型("+value+")";
}

function fgoodsStatus (data,value) {
	if(!value){
		return "";
	}
	var stats_goodsStatus = data.goodsStatus.result ;
	for(index in stats_goodsStatus){
		var goodsStatus = stats_goodsStatus[index];
		if(goodsStatus.dictcode == value){
			return goodsStatus.dictname;
		}
	}
	return  "未知类型("+value+")";
}

function fapplyStatus(data,value){
	if(!value){
		return "";
	}
	var stats_approves = data.approves.result ;
	for(index in stats_approves){
		var approves = stats_approves[index];
		if(approves.dictcode == value){
			return approves.dictname;
		}
	}
	return  "未知类型("+value+")";
}
function ftime (value){
	if(!!value){
		return value.year+"-"+value.monthValue+"-"+value.dayOfMonth+" "+value.hour+":"+value.minute+":"+value.second;
	}
	return "";
}

//弹窗
function skuSelector(idx){
	detailId = idx;
	selSkuPage();	
	$("#choose_sku_modal").modal();	
}

var page = {
	pageSize : 5,
	pageNo : 1
}

var goodsResult ; 

var detailId;
var selPage = null;
function selSkuPage(){ 
	
		params = {
			pageSize : this.page.pageSize,
			pageNo : this.page.pageNo,
			
			itemname : $("#itemname").val(),
			itemno : $("#itemno").val(),
			customername:$("#customername").val()
			
		}
		if(!!selPage){
			selPage.page("remote",0,params);
		}else{
			selPage = $("#page").page({
				remote: {
					url: _this.urlItems.queryGoodsURL,
			        params: params,
			        success: function (data, pageIndex) {
			        	if(data.code==10000){
			        		var pageResult = data.result;	
			        		goodsResult = pageResult.itemList ;
			        		for(var index =0;index<goodsResult.length;index++){
		        				var tmpGoods = goodsResult[index];
		        				if(!!goodsResult[index]["currency"]){
		        					goodsResult[index]["currency"] = fCurrencysUtil(data,tmpGoods["currency"]);
		        				}
		        				
		        				if(!!goodsResult[index]["docketstatus"]){
		        					goodsResult[index]["docketstatus"] = fgoodsStatus(data,tmpGoods["docketstatus"]);
		        				}
		        				
		        				if(!!goodsResult[index]["applystatus"]){
		        					goodsResult[index]["applystatus"] = fapplyStatus(data,tmpGoods["applystatus"]);
		        				}
		        				
		        				if(!!goodsResult[index]["applytime"]){
		        					goodsResult[index]["applytime"] = ftime(tmpGoods["applytime"]);
		        				}
			        		}	
			        		
			        		$("#data_grid").html($("#dataTmp1").render(goodsResult));
			        	}else{
			        		 $.alert({
					    	        title: '温馨提示',
					    	        content: '数据查询失败!',
					    	        confirmButtonClass: 'btn-info',
					    	        confirmButton: '确定',
					    	        columnClass: 'col-md-4 col-md-offset-4'
					    	  });
			        	}
			        },error: function (data){
			        	$.alert({
			    	        title: '温馨提示',
			    	        content: '数据查询失败!',
			    	        confirmButtonClass: 'btn-info',
			    	        confirmButton: '确定',
			    	        columnClass: 'col-md-4 col-md-offset-4'
			        	});
			        	return;
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		}
}

function selSkuCode(val){
	var goods;
	for(var index = 0;index<goodsResult.length;index++){
		var tmp = goodsResult[index];
		if(tmp.itemno == val){
			goods = tmp;
			break;
		}		
	}    
	
	$("#choose_sku_modal").modal('hide');	
	
	var params = {
		 detailId : detailId,
		 goodsId  : goods.id
	};
	
	$.ajax({
		url :_this.urlItems.updateSKUByIdURL,
		data : params,
		dataType : "json",
		type : 'post',
		success : function(data) {
			if(data.code =='10000'){
        		$("#sku"+detailId).val(goods.itemno);
        		$("#itemname"+detailId).text(goods.itemname);
        	}else{
        		$.alert({
    	  	        title: '温馨提示',
    	  	        content: data.msg,
    	  	        confirmButtonClass: 'btn-info',
    	  	        confirmButton: '确定',
    	  	        columnClass: 'col-md-4 col-md-offset-4'
    	  	    });
        	}
		}
	});
	
}


//更新数量
function updateQlt(idx,newQty,oldQty){
	params = {
			orderDetailId : idx ,
			qty : newQty			
	}	
	
	//只读状态不调用后台更新
	if($("#qlt"+idx).attr("readonly") == "readonly"){
		return ;
	}
	
	$.ajax({
		url :_this.urlItems.updateQltURL,
		data : params,
		dataType : "json",
		type : 'post',
		success : function(data) {
			if(data.code!=10000){
       		 $.alert({
	  	        title: '温馨提示',
	  	        content: data.msg,
	  	        confirmButtonClass: 'btn-info',
	  	        confirmButton: '确定',
	  	        columnClass: 'col-md-4 col-md-offset-4'
	  	     });
        	 $("#qlt"+idx).val(oldQty);
			}
		}
	});
}

//删除/恢复
function delOrOpen(idx){
	params = {
			orderDetailId : idx 
	}
	
	$.ajax({
		url :_this.urlItems.delOrOpenURL,
		data : params,
		dataType : "json",
		type : 'post',
		success : function(data) {
			 if(data.code == '10001'){
        		  $.alert({
      	  	        title: '温馨提示',
      	  	        content: data.msg,
      	  	        confirmButtonClass: 'btn-info',
      	  	        confirmButton: '确定',
      	  	        columnClass: 'col-md-4 col-md-offset-4'
      	  	      });
        		  $("#isDel"+idx).text("恢复");
        	  }else if(data.code == '10002'){
        		  $.alert({
      	  	        title: '温馨提示',
      	  	        content: data.msg,
      	  	        confirmButtonClass: 'btn-info',
      	  	        confirmButton: '确定',
      	  	        columnClass: 'col-md-4 col-md-offset-4'
      	  	      });
        		  $("#isDel"+idx).text("删除");
        	  }else{
        		  $.alert({
      	  	        title: '温馨提示',
      	  	        content: data.msg,
      	  	        confirmButtonClass: 'btn-info',
      	  	        confirmButton: '确定',
      	  	        columnClass: 'col-md-4 col-md-offset-4'
      	  	     });
        	  }
		}
	});
}

//提交产品备案
function submit(){
	params = {
		awbNo : $("#awbNo").val()
	}
	
	$.ajax({
		url :_this.urlItems.submitURL,
		data : params,
		dataType : "json",
		type : 'post',
		success : function(data) {
			 if(data.code!='10000'){
         		  $.alert({
     	  	        title: '温馨提示',
     	  	        content: data.msg,
     	  	        confirmButtonClass: 'btn-info',
     	  	        confirmButton: '确定',
     	  	        columnClass: 'col-md-4 col-md-offset-4'
     	  	      });
         	 }else{          		   
         		  $.alert({
     	  	        title: '温馨提示',
     	  	        content:"[运单编号:"+data.result.transNo+",订单编号:"+data.result.orderNo+"]已提交成功...",
     	  	        confirmButtonClass: 'btn-info',
     	  	        confirmButton: '确定',
     	  	        columnClass: 'col-md-4 col-md-offset-4'
     	  	      });
         		  
         		  $("#transNo").val(data.result.transNo);
         		  $("#orderNo").val(data.result.orderNo);
         		  //禁用所有操作功能
      		      disable();
      		     
         	 }
		}
	});
}


//提交产品备案
function bak(idx){	
	
	var skuCode = $("#sku"+idx).val() ;
	var awbNo =  $("#awbNo").val();
	var chineseName = $("#chineseName"+idx).text();
	var englishName = $("#englishName"+idx).text();
	var weight = $("#weight").text();
	var url ;
	
	if(skuCode !=''){
		url = basePath+"/packagestats/goodsRecord/skuIndex?itemNo="+skuCode ;
	}else{
		url = basePath+"/packagestats/goodsRecord/statsIndex?parcelOrderDetailVo.chineseName="+chineseName+"&parcelOrderDetailVo.id="+idx+"&parcelOrderDetailVo.englishName="+englishName+"&parcelOrderVo.weight="+weight+"&parcelOrderVo.awbNo="+awbNo ;
	}
	
	window.location.href = url ;	
}


function disable(){
	
	 // $("button[name='submit']").attr({"disabled":true}); 
	 $("button[name='delOrOpen']").attr({"disabled":true});
	 $("button[name='bak']").attr({"disabled":true});
	 $("button[name='sku_choose']").attr({"disabled":true});
	 $("button[name='sku_remove']").attr({"disabled":true});
	  
	 $("input[name='sku']").attr({"readonly":true});       		     
	 $("input[name='qlt']").attr({"readonly":true});
	
}


$(function(){
	
	$("#awbNo").keydown(function(event) {		
		var key = event.keyCode || event.which;
		if (key == 13 || (key == 10 && $.browser.msie && $.browser.version <= 6.0)) {
			$("#po_search").click();
		}
    });
	
	parcelorderCtrl.parcelorder.event();
});
